package com.tongji.housediy.view.components
{
	import com.tongji.housediy.events.commonEvent.ChangeData3D;
	import com.tongji.housediy.events.commonEvent.ExchangeDataEvent;
	import com.tongji.housediy.events.commonEvent.SetCompStateEvent;
	import com.tongji.housediy.model.CompEnableStateModel;
	import com.tongji.housediy.model.ExchangeDataModel;
	import com.tongji.housediy.vo.ExchangeData;
	
	import flash.display.DisplayObject;
	import flash.display.GraphicsPath;
	import flash.display.GraphicsPathCommand;
	import flash.display.GraphicsSolidFill;
	import flash.display.GraphicsStroke;
	import flash.display.IGraphicsData;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import mx.core.IUIComponent;
	import mx.events.DragEvent;
	import mx.managers.DragManager;
	
	import org.robotlegs.mvcs.Mediator;
	
	/**
	 * 
	 * @author chenchao
	 * 
	 */
	public class MapRoomMediator extends Mediator
	{
		[Inject]
		public var view:MapRoomComp;
		
		[Inject]
		public var model:ExchangeDataModel;
		
		[Inject]
		public var btnStateModel:CompEnableStateModel;
		
		private var arrow:CustomArrow;
		
		private var minX:Number = 10000;
		private var minY:Number = 10000;
		private var maxX:Number = -1;
		private var maxY:Number = -1;
		
		public var divisor:Number;
		
		override public function onRegister():void
		{
			//view.buttonMode = true;
			
			//view.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
			view.addEventListener(DragEvent.DRAG_ENTER, dragEnterViewPointHandler);
			view.addEventListener(DragEvent.DRAG_DROP, dragDropViewPointHandler);
			
			this.addContextListener(ExchangeDataEvent.UPDATE_MAP_GRAPHICS, buildMapRoom, ExchangeDataEvent);
			this.addContextListener(ExchangeDataEvent.UPDATE_MAP_POSITION_ROTAION, updatePositionRotationHandler, ExchangeDataEvent);
			this.addContextListener(SetCompStateEvent.RESET_MAP, removeArrowHandler, SetCompStateEvent);
		}
		
		private function removeArrowHandler(event:SetCompStateEvent):void
		{
			if (arrow)
			{
				view.removeChild(arrow);
				arrow = null;
			}
		}
		
		private function buildMapRoom(event:ExchangeDataEvent = null):void
		{
			for (var i:int = 0; i < view.numChildren;)
			{
				var child:DisplayObject = view.removeChildAt(i);
				child = null;
			}
			
			for each (var exchangeData:ExchangeData in model.relatedExchangeDataVector)
			{
				if (exchangeData.boundingBox.x <= minX)
					minX = exchangeData.boundingBox.x;
				if (exchangeData.boundingBox.y <= minY)
					minY = exchangeData.boundingBox.y;
				if (exchangeData.boundingBox.x + exchangeData.boundingBox.width >= maxX)
					maxX = exchangeData.boundingBox.x + exchangeData.boundingBox.width;
				if (exchangeData.boundingBox.y + exchangeData.boundingBox.height >= maxY)
					maxY = exchangeData.boundingBox.y + exchangeData.boundingBox.height;
			}
			
			divisor = Math.max((maxX - minX) / 223, (maxY - minY) / 164);
			
			view.graphics.clear();
			
			for each (var exchangeData1:ExchangeData in model.relatedExchangeDataVector)
			{
				var graphicsData:Vector.<IGraphicsData>;
				var stroke:IGraphicsData = new GraphicsSolidFill(0xd5c39b);
				var commands:Vector.<int> = new Vector.<int>();
				
				commands.push(GraphicsPathCommand.MOVE_TO);
				
				var count:int = 0;
				
				for (i= 0; i < exchangeData1.relatedFloor.borderLength; i++)
				{
					if (exchangeData1.curveVector.indexOf(count) != -1)
					{
						commands.push(GraphicsPathCommand.CURVE_TO);
						count += 2;
					}
					else
					{
						commands.push(GraphicsPathCommand.LINE_TO);
						count += 1;
					}
				}
				
				var newData:Vector.<Number> = new Vector.<Number>();
				
				for (i = 0; i < exchangeData1.vertices.length; i++)
				{
					if (i % 2 == 0)
						newData.push((exchangeData1.vertices[i] - minX) / divisor);
					else
						newData.push((exchangeData1.vertices[i] - minY) / divisor);
				}
				
				var path:GraphicsPath = new GraphicsPath(commands, newData);
				
				graphicsData = new Vector.<IGraphicsData>();
				graphicsData.push(stroke);
				graphicsData.push(path);
				
				view.graphics.lineStyle(2, 0x443d30);
				view.graphics.drawGraphicsData(graphicsData);
				
				for each (var borderChild:Vector.<Number> in exchangeData1.borderChildVector)
				{
					var length:int = borderChild.length;
					if (length > 0)
					{
						for (i = 0; i < length; i += 8)
						{
							var childComp:Shape = new Shape();
							view.addChild(childComp);
							
							switch (borderChild[i + 6])
							{
								case 0:
								case 1:
									childComp.graphics.lineStyle(2, 0x56b765);
									break;
								case 2:
									childComp.graphics.lineStyle(2, 0xd5c39b);
									break;
							}
							
							childComp.graphics.moveTo((borderChild[i] - minX) / divisor, (-borderChild[i + 1] - minY) / divisor);
							childComp.graphics.lineTo((borderChild[i + 2] - minX) / divisor, (-borderChild[i + 3] - minY) / divisor);
						}
					}
				}
				
				// 绘制内墙
				for (i = 0; i < exchangeData1.initialWallVector.length; i += 4)
				{
					view.graphics.moveTo((exchangeData1.initialWallVector[i] - minX) / divisor, (exchangeData1.initialWallVector[i + 1] - minY) / divisor);
					view.graphics.lineTo((exchangeData1.initialWallVector[i + 2] - minX) / divisor, (exchangeData1.initialWallVector[i + 3] - minY) / divisor);
				}
			}
		}
		
		private function mouseDownHandler(event:MouseEvent):void
		{
			view.startDrag();
			view.removeEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
			view.stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
		}
		
		private function mouseUpHandler(event:MouseEvent):void
		{
			view.stopDrag();
			view.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
		}
		
		private function dragEnterViewPointHandler(event:DragEvent):void
		{
			if (event.dragSource.formats[0] == "characterFormat")
				DragManager.acceptDragDrop(IUIComponent(event.target));
			event.stopImmediatePropagation();
		}
		
		private function dragDropViewPointHandler(event:DragEvent):void
		{
			arrow = new CustomArrow();
			view.addChild(arrow);
			
			arrow.x = view.mouseX;
			arrow.y = view.mouseY;
			
			// 设置 3D 按钮的 enabled 属性，通过其绑定属性
			dispatch(new SetCompStateEvent(SetCompStateEvent.SET_COMP_WANDER_STATE));
			
			this.dispatch(new ExchangeDataEvent(ExchangeDataEvent.CHANGE_WANDER_VIEW,
			new Point(arrow.x * divisor + minX, arrow.y * divisor + minY)));
		}
		
		private function updatePositionRotationHandler(event:ExchangeDataEvent):void
		{
			arrow.rotation = -event.rotation;
			arrow.x = (event.position.x - minX) / divisor;
			arrow.y = (-event.position.y - minY) / divisor;
		}
	}
}